package com.xtz.controller;

import com.sun.org.apache.xerces.internal.impl.dv.xs.AnyURIDV;
import com.xtz.domain.Category;
import com.xtz.dto.AddCategoryDto;
import com.xtz.dto.UpdateCategoryDto;
import com.xtz.service.AdminCategoryService;
import com.xtz.vo.CategoryPageVo;
import com.xtz.vo.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * date: 2022/5/4 16:21
 *
 * @author xietianze
 */
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private AdminCategoryService Service;
    @PostMapping
    public Result addCategory(@RequestBody AddCategoryDto dto){

        if (Objects.isNull(dto)){
            return new Result(0,"参数不能为空",null,null);
        }
        Category category = Service.selectByname(dto.getName());
        if (category!=null){
            return new Result(0,"分类名，已存在",null,null);
        }else{
            Category category1 = new Category();
            BeanUtils.copyProperties(dto,category1);
            Boolean result = Service.addCategory(category1);
            Integer code = result ? 1:0;
            String msg = result? "添加成功":"已存在，添加失败";
            return new Result(code,msg,null,null);
        }

    }
    @PutMapping
    public Result updateCategory(@RequestBody UpdateCategoryDto dto){
        if (Objects.isNull(dto)){
            return new Result(0,"参数不能为空",null,null);
        }
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        Service.updateCategory(category);

        return new Result(1,"吴鹏是我二","",null);
    }

    @DeleteMapping
    public Result deleteCategory(String id){
        if (id == null){
            return new Result(0,"id不能为空",null,null);
        }
        boolean result = Service.deleteCategory(id);
        String msg = result? "删除成功!":"没有该id||当前分类下关联了套餐，不能删除";
        int i = result ? 1 : 0;

        return new Result(i,msg,null,null);
    }

    @GetMapping("/list")
    public Result getList(){
        List<Category> categories = Service.selectAll();
        ArrayList<Category> list = new ArrayList<>();
        return new Result(1, null, categories,null );
    }
    @GetMapping("/page")
    public Result getCategoryPage(@RequestParam(value = "name",required = false) String name,@RequestParam(value = "page",defaultValue = "1")Integer page,
                                  @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        List<Category> categoryPage = Service.getCategoryPage(page, pageSize);
        Long categoryAmount = Service.getCategoryAmount(name);
        CategoryPageVo categoryPageVo = new CategoryPageVo();
        categoryPageVo.setRecords(categoryPage);
        categoryPageVo.setSize(pageSize);
        categoryPageVo.setTotal(categoryAmount);
        categoryPageVo.setCurrent(page);


        return new Result(1,null,categoryPageVo,null);
    }
}
